package com.app.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.db.sql.Order;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.app.common.Result;
import com.app.enums.StatusCodeEnum;
import com.app.model.CommentFileModel;
import com.app.model.CommentModel;
import com.app.model.OrderModel;
import com.app.model.UserModel;
import com.app.service.CommentFileService;
import com.app.service.CommentService;
import com.app.service.OrderService;
import com.app.service.UserService;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.CommentVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 评论
 *
 * @author 16693103420
 * @since 2024-01-22
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private OrderService orderService;

    /**
     * 查询列表
     *
     * @param startTime   根据评论创建时间筛  开始时间
     * @param endTime     根据评论创建时间筛  结束时间
     * @param commentType evaluate==commentType  评价 2好评 3中评 4差评
     * @return
     */
    @GetMapping("/getCommentList")
    public Result getCommentList(@RequestParam(defaultValue = "0") Integer page,
                                 @RequestParam(defaultValue = "10") Integer limit, String startTime, String endTime, Integer commentType) {
        CommentVo commentList = commentService.getCommentList(page, limit, startTime, endTime, commentType);
        return Result.success(commentList);
    }

    /**
     * 根据类型获取评论列表
     *
     * @param relationId  关联评论对象
     * @param commentType 评论类型1.最新 2.好评 3.中评 4.差评
     * @return
     */
    @GetMapping("/getCommentByTypeList")
    public Result getCommentByTypeList(@RequestParam(defaultValue = "0") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit, Integer relationId,
                                       @RequestParam(defaultValue = "1") Integer commentType) {
        CommentVo commentList = commentService.getCommentByTypeList(page, limit, relationId, commentType);
        return Result.success(commentList);
    }

    //评论详情
    @GetMapping("/getCommentById")
    public Result getCommentById(Integer id) {
        CommentModel comment = commentService.getCommentById(id);
        if (comment != null && comment.getFileId() != null) {
            List<String> url = JSON.parseObject(comment.getFileId(), new TypeReference<List<String>>() {
            });
            comment.setUrlList(url);
        }
        return Result.success(comment);
    }

    //用户新增评论
    @PostMapping("/userAddComment")
    public Result userAddComment(@RequestBody CommentModel commentModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        String orderId = commentModel.getOrderId();
        QueryWrapper<OrderModel> qw = new QueryWrapper<>();
        qw.eq("order_id", orderId);
        OrderModel order = orderService.getOne(qw);
        if (order == null) {
            return Result.success("没有此订单");
        }
        if (order.getStatus() != 2) {
            return Result.success("您已经评价过此订单");
        }
        commentModel.setOpenId(openId);
        commentService.userAddComment(commentModel);
        return Result.success("添加成功");
    }

    //删除
    @GetMapping("/delComment")
    public Result delComment(Integer id) {
        commentService.removeById(id);
        return Result.success("删除成功");
    }
}

